// field_relate.cpp
// Customizer
// Protea Customizer / Business Logic for managing field relations
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "customizer.h"

//@ SEC 2
//
//@ CHA AddFieldRelation
//@ COD AddFieldRelation
//@ NUM 190
//@ CRI FIELD_NAME
//@ TAB FIELD_DESCRIPTOR Write 
//@ TAB FIELD_NBR_POOL Write
//@ TAB APPL_DEFN Write
//
//@ GEN AddField will create a new field and write a new FIELD_DESCRIPTR record
//
TRANSACTION_CODE( AddFieldRelation ) 
{
	// Declare buffers and init to null
	XCBuffer* TransBuf = NULL;

	//@ GRS Start the transaction
	START_TXN( AddFieldRelation );

		//@ Move supplied buffer information into transaction buffer
		TransBuf = App->CreateBuffer( FMT_FIELD_RELATE, false );
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		//@ GRS Start database transaction
		START_DB_TXN();

			//@ SUB FieldDescVerify to verify input data
			FieldRelateVerify( TransBuf, VERIFY_ADD, &txnvars );

			txnvars.Errors->Check();

			//@ SUB FieldRelateAdd to add new record
			FieldRelateAdd( TransBuf, &txnvars );

			txnvars.buffer->MoveCorresponding( TransBuf, txnvars.Errors );
			App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

			LOG_TRANSACTION();

		END_DB_TXN();
		//@ GRE End Database transaction

	CLEANUP_TXN();
		App->CleanupObjects( 1, &TransBuf );
	END_TXN();
	//@ GRE End the transaction
};







//
//@ CHA DelFieldRelation
//@ COD DelFieldRelation
//@ NUM 190
//@ CRI FIELD_NAME
//@ TAB FIELD_DESCRIPTOR Write 
//@ TAB FIELD_NBR_POOL Write 
//
//@ GEN DelField will delete FIELD_DESCRIPTR records
//
TRANSACTION_CODE( DelFieldRelation ) {

	XCBuffer* TransBuf = NULL;

	//@ GRS Start the transaction
	START_TXN( DelFieldRelation );

		TransBuf = App->CreateBuffer( FMT_FIELD_RELATE, false );
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		START_DB_TXN();
			//@ SUB FieldRelateVerify to verify valid deletion
			FieldRelateVerify( TransBuf, VERIFY_DELETE, &txnvars );

			txnvars.Errors->Check();

			//@ SUB FieldRelateDelete to delete field record
			FieldRelateDelete( TransBuf, &txnvars );

			LOG_TRANSACTION();

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 1, &TransBuf );
	END_TXN();
	//@ GRE End the transaction
};





//
//@ CHA RevFieldRelations
//@ COD RevFieldRelations
//@ NUM 190
//@ CRI FIELD_NAME
//@ TAB FIELD_DESCRIPTOR Write 
//@ TAB FIELD_NBR_POOL Write 
//
//@ GEN RevFieldRelations will return field relation records for a field
//
TRANSACTION_CODE( RevFieldRelations ) {

	XCBuffer* TransBuf = NULL;
	XCBuffer* detail = NULL;
	XCDBStatement* stmt = NULL;

	//@ GRS Start the transaction
	START_TXN( RevFieldRelation );

		TransBuf = App->CreateBuffer( FMT_FIELD_RELATE, false );
		TransBuf->MoveCorresponding( txnvars.buffer, txnvars.Errors );

		START_DB_TXN();
			XCSQLGen* query = txnvars.query;

			//@ SUB FieldRelateVerify to verify valid deletion
			FieldRelateVerify( TransBuf, VERIFY_REVIEW, &txnvars );

			query->MakeSELECT( TransBuf );
			query->AddWHERE( 2, TransBuf->GetField( FLD_APPL_NBR ), TransBuf->GetField( FLD_MSTR_FIELD_NBR ) );

			App->TransResponse( txnvars.result, txnvars.buffer, TRANS_HEADER_TYPE );

			int child = txnvars.buffer->GetChildFormat();
			if (child) {
				detail = App->CreateBuffer( child, true );

				stmt = txnvars.dbtxn->ExecuteSQL( query );
				while (!stmt->Eof()) {
					detail->Clear();
					detail->MoveCorresponding( txnvars.buffer, txnvars.Errors );
					detail->MoveCorresponding( TransBuf, txnvars.Errors, true );

					// Convert field name into field number
					LookupNameFromNumber( &txnvars, TransBuf, detail, FLD_FIELD_NUMBER, FLD_FIELD_NAME, FMT_FIELD_DESCRIPTOR );

					App->TransResponse( txnvars.result, detail, TRANS_DETAIL_TYPE );
					stmt->MoveNext();
				};
			};

		END_DB_TXN();

	CLEANUP_TXN();
		App->CleanupObjects( 3, &TransBuf, &stmt, &detail );
	END_TXN();
	//@ GRE End the transaction
};
